package io.input;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;

/**
 * @author mubei
 * @date 2021/12/3
 * @description
 */
public class TestFileInputStream {



    @Test()
    public void Test1()   {
        String filePath = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\io/test.txt";
        File file = new File(filePath);
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            if(!file.exists()) {
                file.createNewFile();
            }else{
                System.out.println("文件以存在");
                System.out.println(file.getAbsolutePath());
                 fileInputStream = new FileInputStream(filePath);

                while((readData = fileInputStream.read()) != -1 ){
                    System.out.println((char)readData);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    @Test()
    public void Test2() {
        String filePath = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\io/test.txt";
        File file = new File(filePath);
        int readData = 0;
        FileInputStream fileInputStream = null;
        byte[] buf = new byte[5];
        try {
            if(!file.exists()) {
                file.createNewFile();
            }else{
                System.out.println("文件以存在");
                System.out.println(file.getAbsolutePath());
                fileInputStream = new FileInputStream(filePath);

                while((readData = fileInputStream.read(buf)) != -1 ){
                    System.out.print(new String(buf,0,readData));
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
